//
//  UIView+JSAniamtion.m
//  JSFoundationProject
//
//  Created by mac on 17/6/28.
//  Copyright © 2017年 JSON. All rights reserved.
//

#import "UIView+JSAniamtion.h"

@implementation UIView (JSAniamtion)

static char *showMaskKey1;
static char *showMaskKey2;
/**
 *  从上往下推出
 */
-(void)transformAnimation1{
    
    UIView *mask    = objc_getAssociatedObject(self, &showMaskKey1);
    if (!mask) {
        CGRect rect     = [self convertRect:self.frame toViewOrWindow:nil];
        
        JSLog(@"transformAnimation1 %@", NSStringFromCGRect(rect));
        mask             = [[UIView alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, SCREEN_WIDTH - rect.origin.x, SCREEN_HEIGHT - rect.origin.y)];
        mask.backgroundColor = [UIColor blackColor];
        mask.alpha = 0.5;
        objc_setAssociatedObject(self, &showMaskKey1, mask, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        JS_WeakSelf(self)
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            //            JS_StrongSelf(weakself)
            [weakself hidenAnimation1];
        }];
        [mask addGestureRecognizer:tap];
    }
    mask.alpha = 0;
    [kAppWindow addSubview:mask];
    [kAppWindow addSubview:self];
    CGRect oldFrame = self.frame;
    self.layer.anchorPoint = CGPointMake(1, 0);
    self.frame = oldFrame;
    self.transform = CGAffineTransformMakeScale(1.f, 0.01f);
    self.alpha     = 0;
    [UIView animateWithDuration:0.25f animations:^{
        self.transform = CGAffineTransformIdentity;
        self.alpha = 1.f;
        mask.alpha = 0.5;
        if (self.showAnimationCompletion) {
            self.showAnimationCompletion(self);
        }
    }];
    
}
-(void)hidenAnimation1{
    UIView *mask    = objc_getAssociatedObject(self, &showMaskKey1);
    
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha     = 0;
        mask.alpha     = 0;
        self.transform = CGAffineTransformMakeScale(1.f, 0.01f);
        
    } completion:^(BOOL finished) {
        if (mask) {
            [mask removeFromSuperview];
            [self removeAssociatedValues];
        }
        [self removeFromSuperview];
        if (self.hideAnimationCompletion) {
            self.hideAnimationCompletion(self);
        }
    }];
}

- (void)showMaskView{
    UIView *maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    objc_setAssociatedObject(self, &showMaskKey2, maskView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    maskView.alpha = 0;
    maskView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    UIView *windown = [UIApplication sharedApplication].keyWindow;
    [windown addSubview:maskView];
    [windown addSubview:self];
    self.mj_y = SCREEN_HEIGHT;
    [UIView animateWithDuration:0.4 animations:^{
        // self.transform = CGAffineTransformTranslate(self.transform, 0, self.frame.size.height);
        self.transform = CGAffineTransformMakeTranslation(0, -self.height);
        maskView.alpha = 1;
    } completion:^(BOOL finished) {
        
        if (self.showAnimationCompletion) {
            self.showAnimationCompletion(self);
        }
    }];
    
}

- (void)hidenMaskView{
    UIView *maskView = objc_getAssociatedObject(self, &showMaskKey2);
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformTranslate(self.transform, 0, self.frame.size.height);
        maskView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        if (maskView) {
            [maskView removeFromSuperview];
            [self removeAssociatedValues];
        }else{
            
        }
        if (self.hideAnimationCompletion) {
            self.hideAnimationCompletion(self);
        }
        objc_removeAssociatedObjects(self);
    }];
}

// 透明度渐变
- (void)showMaskViewAlpha{
    
    UIView *maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self.clickMaskHiden) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [maskView addGestureRecognizer:tap];
    }
    objc_setAssociatedObject(self, &showMaskKey2, maskView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    maskView.alpha = 0;
    maskView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    UIView *windown = [UIApplication sharedApplication].keyWindow;
    [windown addSubview:maskView];
    [maskView addSubview:self];
    [UIView animateWithDuration:0.4 animations:^{
        
        maskView.alpha = 1;
    } completion:^(BOOL finished) {
        if (self.showAnimationCompletion) {
            self.showAnimationCompletion(self);
        }
    }];
}

- (void)hidenMaskViewAlpha:(BOOL)animation{
    
    UIView *maskView = objc_getAssociatedObject(self, &showMaskKey2);
    if (!animation) {
        maskView.alpha = 0;
        if (maskView) {
            [maskView removeFromSuperview];
            [self removeAssociatedValues];
        }
        
        if (self.hideAnimationCompletion) {
            self.hideAnimationCompletion(self);
        }
        [self removeFromSuperview];
        return;
    }
    [UIView animateWithDuration:0.3 animations:^{
        maskView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        if (maskView) {
            [maskView removeFromSuperview];
        }
        if (self.hideAnimationCompletion) {
            self.hideAnimationCompletion(self);
        }
        //        objc_removeAssociatedObjects(self);
    }];
}

/** 水波纹 */
- (void)js_beginRippleAnimation{
    
    /*! 定义个转场动画 */
    CATransition *transition = [CATransition animation];
    /*! 转场动画持续时间 */
    transition.duration = 0.8;
    // 波纹效果
    transition.type = @"rippleEffect";
    // 方向
    transition.subtype = kCATransitionFromLeft;
    // 动画节奏（先慢后快再慢）
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    /*! 在动画执行完时是否被移除 */
    transition.removedOnCompletion = YES;
    /*! 添加动画（转场动画是添加在层上的动画） */
    [self.layer addAnimation:transition forKey:nil];
    
    
}
/** 先大后小效果 */
-(void)js_alertAnimation {
    
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.15;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [self.layer addAnimation:animation forKey:nil];
}

- (void)setShowAnimationCompletion:(void (^)(UIView *))showAnimationCompletion{
    objc_setAssociatedObject(self, @selector(showAnimationCompletion), showAnimationCompletion, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(UIView *))showAnimationCompletion{
    return objc_getAssociatedObject(self, @selector(showAnimationCompletion));
}
//static char khideAnimationCompletion;
- (void)setHideAnimationCompletion:(void (^)(UIView *))hideAnimationCompletion{
    objc_setAssociatedObject(self, @selector(hideAnimationCompletion), hideAnimationCompletion, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(UIView *))hideAnimationCompletion{
    return objc_getAssociatedObject(self, @selector(hideAnimationCompletion));
}
- (void)setClickMaskHiden:(BOOL)clickMaskHiden{
    objc_setAssociatedObject(self, @selector(clickMaskHiden), @(clickMaskHiden), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)clickMaskHiden{
    NSNumber *num = objc_getAssociatedObject(self, @selector(clickMaskHiden));
    return num.boolValue;
}
-(void)tapClick:(UITapGestureRecognizer *)sender {
    if (self.clickMaskHiden) {
        [self hidenMaskViewAlpha:YES];
    }
}
@end
